﻿using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Configuration;
using Donal.Xms.Common;
using Donal.Xms.API.OAuth;

namespace Donal.Xms.Web.UI
{
    public partial class UserPage : BasePage
    {
        public Model.users userModel;
        public Model.user_groups groupModel;
        private BLL.spec_prices spec_Prices = new BLL.spec_prices();

        /// <summary>
        /// 获取差异价格
        /// 1、规格--固定价格--会员
        /// 2、规格--固定价格--组别
        /// 3、规格--百分比--会员
        /// 4、规格--百分比--组别
        /// 5、商品--百分比--会员（商品与品类无法设置固定价）
        /// 6、商品--百分比--组别（商品与品类无法设置固定价）
        /// 7、品类--百分比--会员（商品与品类无法设置固定价）
        /// 8、品类--百分比--组别（商品与品类无法设置固定价）
        /// 9、会员组别--百分比
        /// 10、游客或登录--百分比（显示价格时）
        /// </summary>       
        /// <param name="product_title">商品名称</param>
        /// <param name="goods_title">规格名称</param>
        /// <param name="discount_remark">返回折扣规格</param>
        /// <param name="category_title">品类名称</param>
        /// <param name="categoryId">品类ID</param>
        /// <param name="productId">商品ID</param>
        /// <param name="goodsId">规格ID</param>        
        /// <param name="orgPrice">商品原价</param>        
        public decimal GetSpecPrice(int goodsId, string goods_title, int productId, string product_title, int categoryId, string category_title, decimal orgPrice, out string discount_remark)
        {
            if (IsUserLogin())
            {
                //获得登录用户信息
                userModel = GetUserInfo();

                userModel.company = new BLL.company().GetModel(userModel.parent_id);
                if (userModel.company == null)
                {
                    userModel.company = new Model.company();

                    groupModel = new BLL.user_groups().GetModel(userModel.group_id);//如果还没有公司信息取用户组别
                    if (groupModel == null)
                    {
                        groupModel = new Model.user_groups();
                    }
                }
                else
                {
                    groupModel = new BLL.user_groups().GetModel(userModel.company.group_id);//如果已认证公司，取公司组别
                    if (groupModel == null)
                    {
                        groupModel = new Model.user_groups();
                    }
                }

                //登录后
                if (userModel.parent_id > 0 && userModel.company.status == 0)//已认证
                {                    
                    return spec_Prices.GetSpecPrice(true, goodsId, goods_title, productId, product_title, categoryId, category_title, userModel.company.id, userModel.company.companyName, userModel.company.group_id, groupModel.title, groupModel.discount, orgPrice, out discount_remark);
                }
            }

            //未登录
            if (uconfig.visitors_price_show == 1)
            {                
                return spec_Prices.GetSpecPrice(false, goodsId, goods_title, productId, product_title, categoryId, category_title, 0, "", 0, "", uconfig.visitors_discount, orgPrice, out discount_remark);
            }

            discount_remark = "不显示价格";
            return 0;
        }


        #region 重写父类的虚方法,此方法将在Init事件前执行
        /// <summary>
        /// 重写父类的虚方法,此方法将在Init事件前执行
        /// </summary>
        protected override void ShowPage()
        {
            this.Init += new EventHandler(UserPage_Init); //加入IInit事件
        }
        #endregion               

        #region OnInit事件,检查用户是否登录
        /// <summary>
        /// OnInit事件,检查用户是否登录
        /// </summary>
        void UserPage_Init(object sender, EventArgs e)
        {
            string url = "http://" + HttpContext.Current.Request.Url.Authority.ToLower() + HttpContext.Current.Request.RawUrl;

            if (url.IndexOf("login.html") > 0 || url.IndexOf("register.html") > 0)
            {
                Utils.WriteCookie(DTKeys.COOKIE_URL_REFERRER, linkurl("index")); //记住上一页面
            }
            else
            {
                Utils.WriteCookie(DTKeys.COOKIE_URL_REFERRER, url); //记住上一页面
            }

            if (!IsUserLogin())
            {
                //跳转URL
                HttpContext.Current.Response.Redirect(linkurl("login"));
                return;
            }

            //获得登录用户信息
            userModel = GetUserInfo();

            if (userModel.company == null)
            {
                userModel.company = new Model.company();

                groupModel = new BLL.user_groups().GetModel(userModel.group_id);//如果还没有公司信息取用户组别
                if (groupModel == null)
                {
                    groupModel = new Model.user_groups();
                }
            }
            else
            {
                groupModel = new BLL.user_groups().GetModel(userModel.company.group_id);//如果已认证公司，取公司组别
                if (groupModel == null)
                {
                    groupModel = new Model.user_groups();
                }
            }

            InitPage();
        }
        #endregion

        #region 构建一个虚方法，供子类重写
        /// <summary>
        /// 构建一个虚方法，供子类重写
        /// </summary>
        protected virtual void InitPage()
        {
            //无任何代码
        }
        #endregion

        #region JS提示
        #region JS提示============================================
        /// <summary>
        /// 添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        protected void JscriptMsg(string msgtitle, string url)
        {
            string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }
        /// <summary>
        /// 带回传函数的添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="callback">JS回调函数</param>
        protected void JscriptMsg(string msgtitle, string url, string callback)
        {
            string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", " + callback + ")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }
        #endregion
        #endregion
    }
}